const { model } = require("mongoose");

const { parkingsModel } = require('./models/parkingsModel');
//数据渲染
module.exports.getparkings =async function({pageSize,currentPage,lessorsId,state}){
  if(state){
    const total = await (await parkingsModel.find({lessorsId,state})).length;
    const pages = Math.ceil(total/pageSize);
      const ingetparks = await parkingsModel.find({lessorsId,state}).populate('lessorsId') 
      .limit(pageSize-0)
     .skip((currentPage-1)*pageSize)
     return {ingetparks,total,pages}
  }else{
    const total = await (await parkingsModel.find({lessorsId})).length;
    const pages = Math.ceil(total/pageSize);
    const ingetparks = await parkingsModel.find({lessorsId}).populate('lessorsId') 
    .limit(pageSize-0)
   .skip((currentPage-1)*pageSize)
   return {ingetparks,total,pages}
   }
}
//查询得到的数据
module.exports. searchgetparkings=async function(park){
    if(park.state){
    const total = await (await parkingsModel.find({[park.seatchtype]: { $regex:park.seatchval, $options: '$i' },lessorsId:park.lessorsId,state:park.state})).length;
    const pages = Math.ceil(total/park.pageSize);
    const ingetparks = await parkingsModel.find({[park.seatchtype]: {$regex:park.seatchval, $options: '$i' },lessorsId:park.lessorsId,state:park.state})
    .limit(park.pageSize-0)
    .skip((park.currentPage-1)*park.pageSize)
    return {ingetparks,total,pages}
      }else{
    const total = await (await parkingsModel.find({[park.seatchtype]: { $regex:park.seatchval, $options: '$i' },lessorsId:park.lessorsId})).length;
    const pages = Math.ceil(total/park.pageSize);
    const ingetparks = await parkingsModel.find({[park.seatchtype]: {$regex:park.seatchval, $options: '$i' },lessorsId:park.lessorsId})
    .limit(park.pageSize-0)
    .skip((park.currentPage-1)*park.pageSize)
    return {ingetparks,total,pages}
    }
}
//删除
module.exports.delparkings = async function ({ _id, isDelete }) {
    await parkingsModel.updateOne({ _id }, { isDelete })
    return await parkingsModel.findById(_id);

}

//修改
module.exports.xgindex = async function (park) {
    return await parkingsModel.findById(park._id);
}

module.exports.addParkings = async function (newParking) {
    return await parkingsModel.create(newParking)
}


module.exports.auditParkings = async function (msg) {
    const arr = await parkingsModel.find().populate('lessorsId')
    return arr
}

module.exports.getState = async function (msg) {
    const data = await parkingsModel.updateOne({ _id: msg._id }, { state: msg.state });
    return data
}
//修改车位

module.exports.xginPark = async function ({ _id, carNumber, price, property, cycle,state}) {
    await parkingsModel.updateOne({ _id }, { carNumber, price, property, cycle,state})
    return await parkingsModel.findById(_id); 
}


